package com.dcoinpay.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dcoinpay.common.aop.annotation.LogAnnotation;
import com.dcoinpay.common.utils.DataResult;
import com.dcoinpay.sys.entity.Coin;
import com.dcoinpay.sys.service.CoinService;
import com.dcoinpay.sys.service.HttpSessionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 币种
 *
 * @author dcoinpay
 * @date 2020-07-02 22:20:34
 */
@Controller
@RequestMapping("/sys/coin")
@Api(tags = "币种管理")
public class CoinController {
	@Autowired
	private CoinService coinService;
	@Autowired
	private HttpSessionService httpSessionService;

	/**
	 * 跳转到页面
	 */
	@GetMapping("/listview")
	public String listview() {
		return "sys/coin/list";
	}

	@ApiOperation(value = "新增币种")
	@PostMapping("/add")
	@LogAnnotation(title = "币种", action = "新增币种")
	@RequiresPermissions("sys:coin:add")
	@ResponseBody
	public DataResult<Coin> add(@RequestBody Coin coin) {

		if (StringUtils.isEmpty(coin.getNodeId())) {
			return DataResult.fail("节点不能为空");
		}

		if (coin.getExchangeFlag() == null) {
			coin.setExchangeFlag(0);
		}
		if (coin.getStatus() == null) {
			coin.setStatus(0);
		}
		if (coin.getCurrentCollectFlag() == null) {
			coin.setCurrentCollectFlag(0);
		}

		coin.setCreateBy(httpSessionService.getCurrentUserId());
		coinService.save(coin);
		return DataResult.success();
	}

	@ApiOperation(value = "删除币种")
	@LogAnnotation(title = "币种", action = "删除币种")
	@DeleteMapping("/delete")
	@RequiresPermissions("sys:coin:delete")
	@ResponseBody
	public DataResult<Coin> delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		coinService.removeByIds(ids);
		return DataResult.success();
	}

	@ApiOperation(value = "更新币种")
	@LogAnnotation(title = "币种", action = "更新币种")
	@PutMapping("/update")
	@RequiresPermissions("sys:coin:update")
	@ResponseBody
	public DataResult<Coin> update(@RequestBody Coin coin) {
    	if(coin.getExchangeFlag() == null) {
    		coin.setExchangeFlag(0);
    	}
    	if(coin.getStatus() == null) {
    		coin.setStatus(0);
    	}
    	if(coin.getCurrentCollectFlag() == null) {
    		coin.setCurrentCollectFlag(0);
    	}
		coinService.updateById(coin);
		return DataResult.success();
	}

	@ApiOperation(value = "查询币种")
	@PostMapping("/list")
	@RequiresPermissions("sys:coin:list")
	@ResponseBody
	public DataResult<Object> findListByPage(@RequestBody Coin coin) {
		IPage<Coin> iPage = coinService.pageInfo(coin);
		return DataResult.success(iPage);
	}

}
